#include "wx_pch.h"
#include "Playground.h"

#ifndef WX_PRECOMP
	//(*InternalHeadersPCH(Playground)
	#include <wx/intl.h>
	#include <wx/string.h>
	//*)
#endif
//(*InternalHeaders(Playground)
//*)

//(*IdInit(Playground)
const long Playground::ID_BUTTON_MIC = wxNewId();
const long Playground::ID_STATICTEXT8 = wxNewId();
const long Playground::ID_GAUGE1 = wxNewId();
const long Playground::ID_TEXTCTRL1 = wxNewId();
const long Playground::ID_BUTTON1 = wxNewId();
const long Playground::ID_PANEL1 = wxNewId();
//*)


BEGIN_EVENT_TABLE(Playground,wxDialog)
	//(*EventTable(Playground)
	//*)
END_EVENT_TABLE()

Playground::Playground(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size) {

	//(*Initialize(Playground)
	wxBoxSizer* BoxSizer2;
	wxBoxSizer* BoxSizer1;
	wxStaticBoxSizer* StaticBoxSizer1;

	Create(parent, wxID_ANY, _("Playground"), wxDefaultPosition, wxDefaultSize, wxCAPTION|wxDEFAULT_DIALOG_STYLE|wxSYSTEM_MENU|wxRESIZE_BORDER|wxCLOSE_BOX|wxMAXIMIZE_BOX, _T("wxID_ANY"));
	SetClientSize(wxSize(261,33));
	BoxSizer1 = new wxBoxSizer(wxHORIZONTAL);
	Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxSize(264,23), wxTAB_TRAVERSAL, _T("ID_PANEL1"));
	BoxSizer2 = new wxBoxSizer(wxVERTICAL);
	StaticBoxSizer1 = new wxStaticBoxSizer(wxHORIZONTAL, Panel1, _("Record"));
	Button1 = new wxButton(Panel1, ID_BUTTON_MIC, _("Start/Stop Mic"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON_MIC"));
	StaticBoxSizer1->Add(Button1, 0, wxEXPAND|wxALIGN_LEFT|wxALIGN_TOP, 0);
	txtMicWorking = new wxStaticText(Panel1, ID_STATICTEXT8, _("..."), wxDefaultPosition, wxSize(59,13), wxALIGN_CENTRE, _T("ID_STATICTEXT8"));
	StaticBoxSizer1->Add(txtMicWorking, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Gauge1 = new wxGauge(Panel1, ID_GAUGE1, 100, wxDefaultPosition, wxSize(188,28), 0, wxDefaultValidator, _T("ID_GAUGE1"));
	StaticBoxSizer1->Add(Gauge1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer2->Add(StaticBoxSizer1, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	txtDebug = new wxTextCtrl(Panel1, ID_TEXTCTRL1, _("Debug window\n"), wxDefaultPosition, wxSize(478,223), wxTE_MULTILINE, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	txtDebug->SetBackgroundColour(wxColour(255,255,202));
	BoxSizer2->Add(txtDebug, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	butClearDebug = new wxButton(Panel1, ID_BUTTON1, _("Clear debug"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
	BoxSizer2->Add(butClearDebug, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Panel1->SetSizer(BoxSizer2);
	BoxSizer2->SetSizeHints(Panel1);
	BoxSizer1->Add(Panel1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SetSizer(BoxSizer1);
	BoxSizer1->SetSizeHints(this);
	Center();

	Connect(ID_BUTTON_MIC,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Playground::OnButton1Click);
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Playground::OnButClearDebugClick);
	//*)

//	_processsound = new ProcessSound(Gauge1);
//
    _isStopped = true;
//    _bitsPerSample = 16;
//    _samplesPerSecond = Wave::BASE_SAMPLES_SEC;
//    _fftPoints = FFT_POINTS * 4;
//    _samplesPerBuf = FFT_POINTS * 4;
//
//    _processsound->SetFormat (_samplesPerBuf,	_samplesPerSecond, _fftPoints, _bitsPerSample);
//    _processsound->ReInit	();
//    // could be	changed	by ReInit ()
//    _processsound->GetFormat (_samplesPerSecond, _bitsPerSample);
  txtMicWorking->SetLabel(wxString::Format(_("Mic ready")));
}

Playground::~Playground()
{
	//(*Destroy(Playground)
//	_processsound->Kill ();
  soundInput.kill();
	//*)
}


void Playground::OnButton1Click(wxCommandEvent& event){
  if (_isStopped)		{
          txtMicWorking->SetLabel(wxString::Format(_("Mic on")));
          soundInput.startRecording(Gauge1,txtDebug);
          _isStopped = false;
          //while(!_isStopped) {
            //int volume = soundInput.getVolume();
            //Gauge1->SetValue(volume);
           // txtDebug->AppendText(wxString::Format(_("%d\n"), volume));
          //}

  } else {
      Gauge1->SetValue(0);
      soundInput.stopRecording();
//			_processsound->Stop ();
			txtMicWorking->SetLabel(wxString::Format(_("Mic off")));
			txtDebug->AppendText(_("Mic off\n"));
			_isStopped = true;
   }
}

void Playground::OnButClearDebugClick(wxCommandEvent& event)
{
  txtDebug->Clear();
}
